//
//  C_RedioViewController.m
//  音悦咖
/*
  Created by Best on 15/7/15.
 http://fm.api.ttpod.com/vipradiosong?userid=0000001&num=150&tagid=9&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music
 
 
 
 */
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "C_RedioViewController.h"
#import "C_RedioCollectionViewCell.h"
#import "L_Singleton.h"
#import "C_RedioPlayerModel.h"
#import "L_footPlaytabBar.h"

#define API_REDIO_LIST_URL_FOUNT   @"http://fm.api.ttpod.com/vipradiosong?userid=0000001&num=150&tagid="
#define API_REDIO_LIST_URL_BLACK   @"&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C1&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=07afc257564998d95c30d3961b852009acbd4bc7&idfa=344D8202-B8A1-4025-8A03-D1D21F9B1723&utdid=VZ9EyvXI9JADAKFnFQwx0zo5&alf=201200&bundle_id=com.ttpod.music"

typedef void(^BLOCK)(NSMutableArray *arr);

@interface C_RedioViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>


@property (nonatomic, retain) NSMutableArray *titleArr;
@property (nonatomic, retain) NSMutableArray *imageArr;
@end

@implementation C_RedioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBA(200, 30, 110, 1);
    self.titleLabel.text = @"电台";
    blackBoubdImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    blackBoubdImageView.userInteractionEnabled = YES;
    
    blackBoubdImageView.image = [UIImage imageWithContentsOfFile:GETBUNDLE_PATH(@"blackgound", @"jpg")];
    [self.view addSubview:blackBoubdImageView];
    
    [self getdate];
    
}

#pragma mark getData
- (void)getdate
{
      [AFNetworkHandler GETWithAFNByURL:self.redio_url completion:^(id result) {
         
          self.titleArr = [RedioModel arrayWithModelByArray:[result objectForKey:@"data"]];
          
          self.imageArr = [C_RedioImageModel arrayWithModelByArray:[self.titleArr[0] dataArr]];
          [self createButton];
          [self createImageCollection];
      }];
}


#pragma 创建Button
- (void)createButton
{
    for (NSInteger i = 0; i < self.titleArr.count; i++) {
        titleButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        CGFloat width = SCREEN_WIDTH / self.titleArr.count;
        CGFloat height = 30.0f;
       
        titleButton.tag = 1000 + i;
       
        [titleButton setTitle:[self.titleArr[i] tag_type_name] forState:(UIControlStateNormal)];
        [titleButton setTitleColor:COLOR_STOCHASTIC  forState:(UIControlStateSelected)];
        [titleButton addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        if (i % 2 == 0) {
            titleButton.frame = CGRectMake(width * i, height + 10, width, height);
        } else {
            titleButton.frame = CGRectMake(width * i, 0, width, height);
        }
        
         [blackBoubdImageView addSubview:titleButton];
    }
}

#pragma mark GetRedioPlayerData
- (void)getRedioPlayerDataWith:(NSString *)tag_id complation:(BLOCK)block
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@", API_REDIO_LIST_URL_FOUNT, tag_id, API_REDIO_LIST_URL_BLACK];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        
       NSMutableArray *tempArr = [C_RedioPlayerModel arrayWithModelByArray:[result objectForKey:@"data"]];
        block(tempArr);
    }];
    
}



#pragma mark Button的点击方法
- (void)buttonAction:(UIButton *)button
{
    
    if (button.tag >= 1000 && button.tag < 1100) {
        self.imageArr = [C_RedioImageModel arrayWithModelByArray:[self.titleArr[button.tag - 1000] dataArr]];
        [imageCollection reloadData];
        titleButton.selected = NO;
        button.selected = YES;
        titleButton = button;
        
        
    }
}


#pragma mark createImagecollection

-(void)createImageCollection
{
    layout = [[C_RedioUICollectionViewLayout alloc] init];
    layout.collectionView.contentSize = CGSizeMake(0, (layout.itemSize.height + layout.minimumLineSpacing) * ([layout.collectionView numberOfItemsInSection:0]+ 2));
    imageCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 200) collectionViewLayout:layout];
    imageCollection.delegate = self;
    imageCollection.tag = 4000;
    imageCollection.dataSource = self;
    imageCollection.showsVerticalScrollIndicator = NO;
    imageCollection.backgroundColor = [UIColor clearColor];
    [blackBoubdImageView addSubview:imageCollection];
    [imageCollection release];
    layout.collectionView.contentOffset = CGPointMake(0, (layout.itemSize.height + layout.minimumLineSpacing) * [layout.collectionView numberOfSections] / 2);
    
    [imageCollection registerClass:[C_RedioCollectionViewCell class] forCellWithReuseIdentifier:@"C_RedioCollectionViewCell"];
    
}
#pragma mark collection Delegate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1000;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    C_RedioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"C_RedioCollectionViewCell" forIndexPath:indexPath];
    cell.imageModel = self.imageArr[indexPath.item];
    cell.backgroundColor = RGBA(arc4random() % 256, arc4random() % 256, arc4random() % 256, 1);
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [blackBoubdImageView setImageWithURL:[self.imageArr[indexPath.item] pic_url_240_200]];
#pragma mark 音乐播放
    
    
      [self getRedioPlayerDataWith:[self.imageArr[indexPath.item] tag_id] complation:^(NSMutableArray *arr) {
          
          
          NSMutableArray *listArr = [NSMutableArray array];
          
          for (C_RedioPlayerModel *redio in arr) {
              
              if ((NSNull *)redio.url_list == [NSNull null]) {
                  
                  NSLog(@"数组为%@", [redio url_list]);
              }
              
              [listArr addObject:redio];
              
          }
          
          if (listArr.count) {
              
              L_Singleton *single = [L_Singleton shareManager];
              L_footPlaytabBar *footTabBar = [L_footPlaytabBar shareTabBar];
              // 传值
              single.song_id = [listArr[indexPath.item] song_id];
              single.index = indexPath.item;
              // 临时列表
              if (single.array == nil) {
                  for (PlayerModel *play in listArr) {
                      [single.array addObject:play];
                      
                  }
                  NSString *url = [[[single.array[indexPath.item] url_list] firstObject] objectForKey:@"url"];
                  [single playWithUrl:url];
                  self.single.playUrl = url;
                  single.state = @"playing";
                  single.index = indexPath.item;
                  
                  
                  footTabBar.footPlayView.songName.text = [single.array[indexPath.item] song_name];
                  footTabBar.footPlayView.songerName.text = [single.array[indexPath.item] singer_name];
                  
                  [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.item] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"headimg@2x.png"]];
                  [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
                  
                  
              } else if ([single.array firstObject] != [arr firstObject]) {
                  single.array = NULL;
                  single.array = [NSMutableArray array];
                  for (PlayerModel *play in arr) {
                      [single.array addObject:play];
                      
                  }
                  
                  NSString *url = [[[single.array[indexPath.item] url_list] objectAtIndex:1] objectForKey:@"url"];
                  
                  [single playWithUrl:url];
                  self.single.playUrl = url;
                  single.state = @"playing";
                  single.index = indexPath.item;
                  footTabBar.footPlayView.songName.text = [single.array[indexPath.item] song_name];
                  footTabBar.footPlayView.songerName.text = [single.array[indexPath.item] singer_name];
                  [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.item] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"headimg@2x.png"]];
                  [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
              } else {
                  single.song_id = [single.array[indexPath.item] song_id];
                  NSString *url = [[[single.array[indexPath.item] url_list] firstObject] objectForKey:@"url"];
                  [single playWithUrl:url];
                  self.single.playUrl = url;
                  single.state = @"playing";
                  single.index = indexPath.item;
                  footTabBar.footPlayView.songName.text = [single.array[indexPath.item] song_name];
                  footTabBar.footPlayView.songerName.text = [single.array[indexPath.item] singer_name];
                  [footTabBar.footPlayView.mainImage setImageWithURL:[NSURL URLWithString:[[[single.array[indexPath.item] mv_list] firstObject] objectForKey:@"pic_url"]] placeholderImage:[UIImage imageNamed:@"headimg@2x.png"]];
                  [footTabBar.footPlayView.playOrPause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
              }

          }
          
          
      }];
    
      

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
